package com.taikang.hmp.hbusiness.doctor.information.action.intface;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.ErrorConstant;
import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.properties.PropertiesHelper;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.hbusiness.doctor.information.bean.Advert;
import com.taikang.hmp.hbusiness.doctor.information.bean.Favorite;
import com.taikang.hmp.hbusiness.doctor.information.bean.Information;
import com.taikang.hmp.hbusiness.doctor.information.service.MessageService;

/**
 * 类说明 :
 * @author :sym
 * @time : 2015-10-10 下午5:53:45
 */
public class MessageAction extends BaseAction{

	private static final long serialVersionUID = 1L;

	private static Logger logger = LoggerFactory.getLogger(MessageAction.class);

	private MessageService messageService;

	public MessageService getMessageService() {
		return messageService;
	}
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
	public String executeAction() {
		if ("getMessageTop".equals(requestDto.getMethod())){
			logger.info("--获取资讯头条: getMessageTitle method --");
			return getMessageTop();
		} else if("getMessageList".equals(requestDto.getMethod())) {
			logger.info("--获取资讯列表: getMessageList method --");
			return getMessageList();
		} else if ("getMessageH".equals(requestDto.getMethod())) {
			logger.info("--html5页面获取资讯详情: getMessage method --");
			return getMessageH();
		} else if ("getMessage".equals(requestDto.getMethod())) {
			logger.info("--获取资讯详情: getMessage method --");
			return getMessage();
		} else if ("addCollectMessage".equals(requestDto.getMethod())) {
			logger.info("-收藏资讯: addCollectMessage method --");
			return addCollectMessage();
		} else if ("getMyCollectionList".equals(requestDto.getMethod())){
			logger.info("-查看收藏资讯列表: getMyCollectionList method --");
			return getMyCollectionList();
		} else if ("deleteCollectMessage".equals(requestDto.getMethod())){
			logger.info("-删除资讯记录: deleteCollectMessage method --");
			return deleteCollectMessage();
		}  else if ("getBanner".equals(requestDto.getMethod())){
			logger.info("-获取首页广告: getBanner method --");
			return getBanner();
		}  else if ("topMessage".equals(requestDto.getMethod())){
			logger.info("-置顶一条收藏记录: topMessage method --");
			return topMessage();
		}	else if ("getBannerDetails".equals(requestDto.getMethod())){
			logger.info("-得到一条banner详情: getBannerDetails method --");
			return getBannerDetails();
		} 	else if ("healthConsultation".equals(requestDto.getMethod())){
			logger.info("-健康养生咨询: topMessage method --");
			return healthConsultation();
		} 	else if ("getLocalState".equals(requestDto.getMethod())){
			logger.info("-健康养生咨询: topMessage method --");
			return getLocalState();
		} 	else if ("addBannerCollection".equals(requestDto.getMethod())){
			logger.info("-收藏Banner: addBannerCollection method --");
			return addBannerCollection();
		} 	else if ("deleteBannerCollection".equals(requestDto.getMethod())){
			logger.info("-删除Banner收藏: deleteBannerCollection method --");
			return deleteBannerCollection();
		} else if ("getCollInfoAndBanner".equals(requestDto.getMethod())){
			logger.info("-查看收藏资讯和Banner列表: getCollInfoAndBanner method --");
			return getCollInfoAndBanner();
		} else if ("getBannerDetail".equals(requestDto.getMethod())){
			logger.info("-获取Banner页面: getBannerDetail method --");
			return getBannerDetail();
		} else {
			throw new TKException("no support method:" + requestDto.getMethod());
		}
	}
	/**
	 * 获取Banner的页面
	 * @return
	 */
	private String getBannerDetail() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		Dto result = new BaseDto();
		if(messageService.check_banner_exist(paramDto)){
			result.put("exist", "1");
		}else{
			result.put("exist", "0");
		}
		result.put("html5", "hdhealth/message/bannerdetail.html?");
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist",result);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	/**
	 * 获取资讯和Banner收藏列表
	 * @return
	 */
	private String getCollInfoAndBanner() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = null;
		if(requestDto.getRequestlist()!=null && !requestDto.getRequestlist().isEmpty()){
			paramDto = requestDto.getRequestlist().get(0);
		}
		paramDto = super.appendRequestBase(paramDto, requestDto);
		List<Dto> resultList = messageService.getfavoriteInfoAndBannerList(paramDto.getAsString("uid"));
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	private String deleteBannerCollection() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		messageService.deladfavoriteList(paramDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	/**
	 * 收藏首页Banner
	 * @return
	 */
	private String addBannerCollection() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		messageService.saveBanner(paramDto);
		logger.info("--------------------------------收藏Banner成功");
		
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	private String getMessage() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		Dto result = new BaseDto();
		if(messageService.check_message_exist(paramDto)){
			result.put("exist", "1");
		}else{
			result.put("exist", "0");
		}
		result.put("html5", "hdhealth/message/messagedetail.html?");
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist",result);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	/**
	 * 得到首页banner详情
	 * @return
	 */
	private String getBannerDetails() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		Advert advert = messageService.getBannerById(paramDto.getAsString("adid"));
		Dto result = new BaseDto();
		result.put("picurl", advert.getImageUrl());
		result.put("title", advert.getTitle());
		result.put("summary", advert.getSummary());
		result.put("dates", advert.getDates());
		result.put("content", advert.getContent());
		result.put("readcount", advert.getReadcount());
		result.put("videoid", advert.getVideoid());
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist",result);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
	/**
	 * 获取首页广告显示
	 * @return
	 */
	private String getBanner() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		List<Dto> resultList = messageService.getBanner(paramDto);
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	private String deleteCollectMessage() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		messageService.delfavoritelList(paramDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	/**
	 * 查看收藏资讯列表
	 * @return
	 */
	private String getMyCollectionList() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = null;
		if(requestDto.getRequestlist()!=null && !requestDto.getRequestlist().isEmpty()){
			paramDto = requestDto.getRequestlist().get(0);
		}
		paramDto = super.appendRequestBase(paramDto, requestDto);
		List<Dto> resultList = messageService.getfavoritelList(paramDto.getAsString("uid"));
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	//得到资讯头条 返回分类 时间 位置
	private String getMessageTop() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		List<Dto> resultList = messageService.getMessageTop(paramDto);
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	//收藏一条资讯
	private String addCollectMessage() {
		boolean error = false;
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		//验证该条资讯是否已经收藏
		if(messageService.check_message_exist(paramDto)){
			logger.info("-----------------------该资讯已经收藏！");
			error = true;
			responseDto.addErrorCode(ErrorConstant.HD_MESSAGE_EXIST);
		}
		if(error){
			super.setDataMap(responseDto);
			return SUCCESS;
		}
		if(!error){
			messageService.saveMessage(paramDto);
			logger.info("-----------------------收藏资讯成功！");
		}
		
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	//得到一条资讯详情
	private String getMessageH() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		Information information = messageService.getInformationById(paramDto.getAsString("informationid"));
		Dto result = new BaseDto();
		result.put("picurl", information.getImage_url());
		result.put("title", information.getTitle());
		result.put("type", information.getType());
		result.put("summary", information.getSummary());
		result.put("dates", information.getDates());
		result.put("content", information.getContent());
		result.put("readcount", information.getReadcount());
		result.put("videoid", information.getVideoid());
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist",result);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	/**
	 * 得到资讯列表
	 * @return
	 */
	private String getMessageList() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = null;
		if(requestDto.getRequestlist()!=null && !requestDto.getRequestlist().isEmpty()){
			paramDto = requestDto.getRequestlist().get(0);
		}
		paramDto = super.appendRequestBase(paramDto, requestDto);
		Dto resultList = messageService.getMessageList(paramDto);
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	/**
	 * 置顶一条消息
	 * @return
	 */
	private String topMessage() {
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		messageService.updateMessageInfo(paramDto);
		
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	/**
	 * 养生咨询
	 * @return
	 */
	public String healthConsultation(){
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		
		List<Dto> resultList = messageService.getHealthConsultation(paramDto);
		Dto resultDto = new BaseDto();
		resultDto.put("resultlist", resultList);
		responseDto.pushResultDto(resultDto);
		//
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
	/**
	 * 图片状态
	 * @return
	 */
	public String getLocalState(){
		ResponseDto responseDto = new ResponseDto();
		String localpic = PropertiesHelper.appHelper.getValue("localpic");
		String localicon = PropertiesHelper.appHelper.getValue("localicon");
		Dto dto = new BaseDto();
		dto.put("localpic", localpic);
		dto.put("localicon", localicon);
		responseDto.pushResultDto(dto);
		super.setDataMap(responseDto);
		return SUCCESS;
	}
	
	


}
